package com.cskaoyan.service.impl;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.cskaoyan.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: InfoServiceImpl
 * Description:
 * date: 2022/6/7 14:45
 *
 * @author bruce
 * @since JDK 8
 */
@Service
public class InfoServiceImpl implements InfoService {

    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Override
    public List<String> queryRolesName(Integer roleId) {
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        if (roleId != null) {
            criteria.andIdEqualTo(roleId);
        }
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        List<String> list = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            String name = marketRole.getName();
            list.add(name);
        }
        return list;
    }

    @Override
    public List<String> queryPermission(Integer roleId) {
        MarketPermissionExample example = new MarketPermissionExample();
        MarketPermissionExample.Criteria criteria = example.createCriteria();
        if (roleId != null) {
            criteria.andRoleIdEqualTo(roleId);
        }
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
        List<String> list = new ArrayList<>();

        for (MarketPermission marketPermission : marketPermissions) {
            String permission = marketPermission.getPermission();
            list.add(permission);
        }
        ArrayList<String> apiList = new ArrayList<>();
        for (String permission : list) {
            if ("*".equals(permission)) {
                return list;
            }
            String api = marketPermissionMapper.selectApiByPermission(permission);
            apiList.add(api);
        }
        return apiList;
    }
}
